#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import unittest
import time
from utility import HTMLTestRunner

def test_all_math_cases():
    work_dir = os.path.abspath(os.path.join(__file__, os.path.pardir))
    test_dir = os.path.join(work_dir, 'test/test_match')
    test_suite = unittest.defaultTestLoader.discover(test_dir, '*.py')

    log_dir = os.path.join(work_dir, 'log')

    last_num = 0
    for path in os.listdir(log_dir):
        if not path.endswith('.html'): continue
        file_name, _ = os.path.splitext(path)
        no = file_name[-1:]
        if int(no) > last_num:
            last_num = int(no)

    file_name_prefix = time.strftime('%Y_%m_%d_%H_%M_%S')
    file_name = file_name_prefix + ('_%d' % (last_num+1)) + '.html'

    log_path = os.path.join(log_dir,  file_name)

    with open(log_path, 'wb') as f:
        test_runner = HTMLTestRunner.HTMLTestRunner(stream=f, title='Test Result')
        test_runner.run(test_suite)

if __name__ == '__main__':
    test_all_math_cases()
